import React, { useState } from "react";
import "antd/dist/antd.css";
import { Card, Input, Button, Spin, message } from "antd";
import axios from 'axios'
import  servicePath  from '../config/apiUrl'

function Login(props) {
  const [userName, setUserName] = useState("admin");
  const [password, setPassword] = useState("admin123");
  const [isLoading, setIsLoading] = useState(false);
  const checkLogin = ()=>{
    setIsLoading(true)
    if(!userName){
        message.error('用户名不能为空')
        return false
    }else if(!password){
        message.error('密码不能为空')
        return false
    }
    let dataProps = {
        'userName':userName,
        'password':password
    }
    axios({
        method:'post',
        url:servicePath.checkLogin,
        data:dataProps,
        withCredentials: false
    }).then(
       res=>{
            setIsLoading(false)
            if(res.data.data==='登录成功'){
                sessionStorage.setItem('openId',res.data.openId)
                sessionStorage.setItem('userName',dataProps.userName)
                message.success('登录成功');
                props.history.push('/index')
            }else{
                message.error('用户名密码错误')
            }
       }
    ).catch(err => {
      console.log(dataProps,'err--login',err);
      
    })

    setTimeout(()=>{
        setIsLoading(false)
    },1000)
  }
  return (
    <div className="login-div">
      <Spin tip="Loading..." spinning={isLoading}>
        <Card
          title="JSPang Blog  System"
          bordered={true}
          style={{ width: 400 }}
        >
        {/* prefix={<Icon type="user" style={{ color: "rgba(0,0,0,.25)" }} />} */}
          <Input
            id="userName"
            value={userName}
            size="large"
            placeholder="Enter your userName"
            onChange={e => {
              setUserName(e.target.value);
            }}
          />
          <br />
          <br />
            {/* prefix={<Icon type="key" style={{ color: "rgba(0,0,0,.25)" }} />} */}
          <Input.Password
            id="password"
            size="large"
            value={password}
            placeholder="Enter your password"
            onChange={e => {
              setPassword(e.target.value);
            }}
          />
          <br />
          <br />
          <br/><br/>
          <Button type="primary" size="large" block onClick={checkLogin} > Login in </Button>
        </Card>
      </Spin>
    </div>
  );
}

export default Login;
